﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;
using WAD.DAL;
using WAD.Model;
using WAD.Model.JSON.Shared;
using WAD.WEB.AppCode;

namespace WAD.WEB.Client2
{
    public partial class Detail : WxJsWebBase
    {
        protected int article_id, advert_id, shareid;
        protected string title, content, url, type, logo, date, click, phone, advert_ids, authorId = "0";
        protected StringBuilder sbAdvert = new StringBuilder();
        protected String host;
        protected MUser mUser;

        protected void Page_Load(object sender, EventArgs e)
        {
            host = "http://" + Request.Url.Host.ToString();
            article_id = Parse.Int("article_id", 0);
            advert_id = Parse.Int("advert_id", 0);
            shareid = Parse.Int("shareid", 0);
            title = Parse.String("url_title", "");
            url = Parse.String("url", "");
            type = Parse.String("type", "");

            if (shareid > 0)
            {
                MShared model = DaoShared.GetInfo(shareid);
                if (model != null)
                {
                    title = model.url_title;
                    advert_id = model.advert_id.Value;
                    url = model.url;
                    logo = model.url_logo;
                    content = title;
                    advert_ids = model.advert_ids;
                    article_id = model.article_id.Value;

                    BindAdvert(advert_ids); //绑定广告列表

                    mUser = DaoUser.GetEntity(model.user_id.Value);
                }
            }

            GetArticle();  //站内链接类型

            if (string.IsNullOrEmpty(logo))
                logo = host + "/client/img/cover.jpg";

            if (logo.ToLower().IndexOf("http") == -1)
                logo = host + logo;  //外部链接的图片(从正文里抓取的)
        }

        void GetArticle()
        {
            if (article_id < 1) return;
            Dictionary<string, object> map = DaoArticle.GetDetail(article_id);
            if (map == null) return;

            title = Convert.ToString(map["title"]);
            logo = Convert.ToString(map["logo"]);
            content = Convert.ToString(map["content"]);
            date = Convert.ToString(map["date"]);
            click = Convert.ToString(map["click"]);
            authorId = Convert.ToString(map["user_id"]);
        }

        /// <summary>
        /// 作废
        /// </summary>
        /// <param name="userid"></param>
        void GetPhone(int userid)
        {
            Dictionary<string, object> m = DaoUser.GetDetail(userid, "phone,articlelogo");
            if (m == null) return;

            phone = m["phone"].ToString();

            //封面图片,如果文章是外部链接，优先从文章里抓取图片，没图片则调用后台设置图片，最后才用系统默认图片
            if (!string.IsNullOrEmpty(logo))
                return;
            if (m["articlelogo"] != DBNull.Value)
                logo = Convert.ToString(m["articlelogo"]);
        }

        protected string GetIntro(int len)
        {
            if (string.IsNullOrEmpty(content))
                return "";
            string intro = QD.WX.Utill.Util.RemoveHtml(content);
            if (intro.Length > len)
                return intro.Substring(0, len);
            return intro;
        }

        //广告列表
        void BindAdvert(string ids)
        {
            DataTable dt = DaoAdvert.GetList(advert_ids, "id,img,url");
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                    sbAdvert.AppendFormat("arrAD[{0}]=new Array('{1}','{2}','{3}');\n", i, dt.Rows[i]["id"], dt.Rows[i]["img"], dt.Rows[i]["url"]);
            }
        }

        /// <summary>
        /// 获取分享信息(APP用)
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        protected string GetShared(string domain, int len, string appendParam)
        {
            MSharedUrl model = new MSharedUrl();
            model.title = title;
            model.description = GetIntro(len);
            model.logo = logo;
            if (model.description.Length == 0)
                model.description = title;

            //将参数打包，发送到跳转域名，通过跳转域名打开实际的分享链接(解决微信、QQ屏蔽问题)
            string param = Server.UrlEncode(string.Format("article_id={0}&advert_id={1}&shareid={2}", article_id, advert_id, shareid));
            model.url = string.Format("{0}/item.html?param={1}{2}", domain, param, appendParam);

            JavaScriptSerializer json = new JavaScriptSerializer();
            return "callback://shared?" + Server.UrlEncode(json.Serialize(model));
        }

        protected string GetShared(string redirectUrl, int len)
        {
            MSharedUrl model = new MSharedUrl();
            model.url = redirectUrl;
            model.title = title;
            model.description = GetIntro(len);
            model.logo = logo;
            if (model.description.Length == 0)
                model.description = title;

            JavaScriptSerializer json = new JavaScriptSerializer();
            return "callback://shared?" + Server.UrlEncode(json.Serialize(model));
        }
    }
}